package com.linchaoen.elema.controller;

import com.linchaoen.elema.model.entity.Address;
import com.linchaoen.elema.model.request.AddrRequest;
import com.linchaoen.elema.service.AddressService;
import com.linchaoen.elema.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/v1/pri/address")
public class AddressController {


    @Autowired
    private AddressService addressService;

    @GetMapping("get_default_addr")
    public JsonData getDefaultAddress(HttpServletRequest request) {
        Integer uid = (Integer) request.getAttribute("uid");
        Address address = addressService.getDefaultAddr(uid);
        return address != null ? JsonData.buildSuccess(address) : JsonData.buildError("无地址");
    }

    @GetMapping("get_all_addr")
    public JsonData getAllAddr(HttpServletRequest request) {
        Integer uid = (Integer) request.getAttribute("uid");
        List<Address> list = addressService.getAllAddr(uid);
        return list != null ? JsonData.buildSuccess(list) : JsonData.buildError("无地址列表");
    }

    @PostMapping("add_addr")
    public JsonData addAddrForNoDefault(HttpServletRequest request, @RequestBody AddrRequest addrRequest) {
        Integer uid = (Integer) request.getAttribute("uid");

        Address addr = new Address();
        addr.setUserName(addrRequest.getUserName());
        addr.setPhone(addrRequest.getPhone());
        addr.setUserId(uid);
        addr.setProvince(addrRequest.getProvince());
        addr.setCity(addrRequest.getCity());
        addr.setCounty(addrRequest.getCounty());
        addr.setAddress(addrRequest.getAddress());
        addr.setIsDefault(addrRequest.getIsDefault());
        addr.setAreaCode(addrRequest.getAreaCode());
        int rows;
        if (addr.getIsDefault()) {
            rows = addressService.addAddr(addr);
            Integer id = addr.getId();
            addressService.setNoDefault(uid, id);
        } else {
            rows = addressService.addAddr(addr);
        }


        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildError("添加地址失败");

    }

    @GetMapping("set_default_addr")
    public JsonData setDefaultAddr(HttpServletRequest request, @RequestParam(value = "id") Integer id) {
        Integer uid = (Integer) request.getAttribute("uid");
        addressService.setDefaultAddr(uid, id);
        addressService.setNoDefault(uid, id);
        return JsonData.buildSuccess();
    }

    @GetMapping("delete_addr")
    public JsonData deleteAddr(HttpServletRequest request, @RequestParam(value = "id") Integer id) {
        Integer uid = (Integer) request.getAttribute("uid");
        int rows = addressService.deleteAddr(uid, id);
        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildError("删除失败");
    }

    @PostMapping("update_addr")
    public JsonData updateAddr(HttpServletRequest request, @RequestBody AddrRequest addrRequest) {
        Integer uid = (Integer) request.getAttribute("uid");

        Address addr = new Address();
        Integer id = addrRequest.getId();
        addr.setUserId(uid);
        addr.setId(addrRequest.getId());
        addr.setUserName(addrRequest.getUserName());
        addr.setPhone(addrRequest.getPhone());
        addr.setProvince(addrRequest.getProvince());
        addr.setCity(addrRequest.getCity());
        addr.setCounty(addrRequest.getCounty());
        addr.setAddress(addrRequest.getAddress());
        addr.setIsDefault(addrRequest.getIsDefault());
        addr.setAreaCode(addrRequest.getAreaCode());

        int rows;
        if (addrRequest.getIsDefault()) {
            rows = addressService.updateAddr(addr);
            addressService.setNoDefault(uid, id);
        } else {
            rows = addressService.updateAddr(addr);
        }

        return rows != 0 ? JsonData.buildSuccess() : JsonData.buildError("修改失败");


    }

}
